package com.hucii.app.utils;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * <p>
 * Title: IntentUtil
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: soufun
 * </p>
 * 
 * @author    mwq
 * @date       2011-12-14
 */
public class IntentUtils {
	/**
	 * 浏览手机相册
	 * 
	 * @return
	 */
	public static Intent createAlbumIntent() {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		Intent chooseIntent = Intent.createChooser(intent, null);
		return chooseIntent;
	}

	/**
	 * 拍照
	 * 
	 * @return
	 */
	public static Intent createShotIntent(File tempFile) {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		Uri uri = Uri.fromFile(tempFile);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		return intent;
	}

	/**
	 * 发短信
	 * 
	 * @param mContext
	 * @param phone
	 * @param content
	 */
	public static void sendSMS(Context mContext, String phone, String content) {
		if (StringUtils.isNullOrEmpty(phone)) {
			return;
		}
		Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"
				+ phone));
		if (mContext.getPackageManager().resolveActivity(sendIntent, 0) == null) {
			Toast.makeText(mContext, "系统不支持此功能", 0).show();
			return;
		}
		sendIntent.putExtra("sms_body", content);
		mContext.startActivity(sendIntent);
	}

	/**
	 * 发邮件
	 * 
	 * @param mContext
	 * @param mail
	 * @param content
	 */
	public static void sendEmail(Context mContext, String mail, String content,
			String subject) {
		Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"
				+ mail));

		if (mContext.getPackageManager().resolveActivity(intent, 0) == null) {
			Toast.makeText(mContext, "系统不支持此功能", 0).show();
			return;
		}
		intent.putExtra(Intent.EXTRA_TEXT, content);
		intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		// intent.setType("text/plain");
		mContext.startActivity(intent);
	}

	/**
	 * 打电话
	 * 
	 * @param mContext
	 * @param phone
	 */
	public static void dialPhone(Context mContext, String phone) {
		dialPhone(mContext, phone, true);
	}

	public static void dialPhone(Context mContext, String phone, boolean isShow) {
		String action = Intent.ACTION_CALL;// 在应用中启动一次呼叫有缺陷,不能用在紧急呼叫上
		if (isShow) {
			action = Intent.ACTION_DIAL;// 显示拨号界面
		}
		if (StringUtils.isNullOrEmpty(phone)) {
			return;
		}
		Intent intent = new Intent(action, Uri.parse("tel:" + phone));
		mContext.startActivity(intent);
	}

	/**
	 * 是否安装了客户端
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean isInstall(Context context, String packageName) {
		PackageManager pckMan;
		pckMan = context.getPackageManager();
		List<PackageInfo> packs = pckMan.getInstalledPackages(0);
		for (int i = 0; i < packs.size(); i++) {
			PackageInfo p = packs.get(i);
			if (packageName.equals(p.packageName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 下载客户端
	 * 
	 * @param context
	 * @param installFileName
	 * @param progressDialog
	 * @return
	 */
	public static Handler getDownLoadHandler(final Context context,
			final String installFileName, final ProgressDialog progressDialog) {
		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					int result = msg.getData().getInt("len");
					progressDialog.setProgress(result);
					break;

				case 1:
					File file = new File(context.getFilesDir() + "/"
							+ installFileName);
					if (file.exists())
						file.delete();
					progressDialog.dismiss();
					break;

				case 2:
					if (progressDialog.getProgress() < 100) {
						progressDialog.setProgress(0);
						for (int i = 1; i < 51; i++) {
							progressDialog.setProgress(i * 2);
							try {
								Thread.sleep(20);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					setupApk(context, installFileName);
					progressDialog.dismiss();
					break;

				case 3:
					Toast.makeText(context.getApplicationContext(),
							"抱歉，下载失败， 请稍后再试", Toast.LENGTH_SHORT).show();
					progressDialog.dismiss();
					break;
				}
			}
		};
		return handler;
	}

	/**
	 * 安装客户端
	 * 
	 * @param context
	 * @param installName
	 */
	public static void setupApk(Context context, String installName) {
		String fileName = context.getApplicationContext().getFilesDir() + "/"
				+ installName;
		Uri uri = Uri.fromFile(new File(fileName));
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		context.startActivity(intent);
	}

	/**
	 * 隐藏软键盘
	 * 
	 * @param activity
	 *            要隐藏软键盘的activity
	 */
	public static void hideSoftKeyBoard(Activity activity) {
		final View v = activity.getWindow().peekDecorView();
		if (v != null && v.getWindowToken() != null) {
			try {
				((InputMethodManager) activity
						.getSystemService(Context.INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(activity.getCurrentFocus()
								.getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void setItems(Context mContext, String title,
			final String[] items, final TextView tv) {

		String name = tv.getText().toString().trim();
		int i = getItem(items, name);

		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(title).setSingleChoiceItems(items, i,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						tv.setText(items[which]);
						dialog.dismiss();
					}
				});
		builder.create();
		builder.show();
	}

	public static int getItem(String[] items, String name) {
		for (int i = 0; i < items.length; i++) {
			if (items[i].equals(name)) {
				return i;
			}
		}
		return 0;
	}

	public static void shareList(Context mContext, String title, String content) {
		Intent intent = new Intent(Intent.ACTION_SEND);

		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, title);
		intent.putExtra(Intent.EXTRA_TEXT, content);
		mContext.startActivity(Intent.createChooser(intent, title));
	}
}
